function F = GetFeatures(P,S,mode, action) % get features of state S for player P

    % feature vector variables
    global x_tiles;
    global y_tiles; 
    global x_tile_width;
    global y_tile_width;
    global angle_tiles;
    global angle_tile_width;
    global distance_tiles;
    global distance_tile_width;
    global num_tilings;
    
    global num_players_r;
    global num_players_b;
    
    global record_states;
    global state_set;
    
    if(P{2}=='r')
        num_players = num_players_r;
        num_opponents = num_players_b;
    else
        num_players = num_players_b;
        num_opponents = num_players_r;
    end
    
    distances_vec = {0 0 0 0 0 0 0 0 0 0};
    angles_vec = {0 0 0 0 0 0 0 0 0 0};
    
    % get the ball object
    B = S{1}; 
    B_X=B{1}(1);
    B_Y=B{1}(2);
    absolute_x = B_X+60; % x ranges from -60 to 60 so normalizing to get a value between 0 and 120
    absolute_y = B_Y + 30; % x ranges from -30 to 30 so normalizing to get a value between 0 and 60
    % obtain features
    
    % distance of ball from goal
    
    ball_x = xFeatures(absolute_x, num_tilings, x_tiles, x_tile_width);
    offset = num_tilings*(x_tiles+1) - 1;
    
    ball_y = xFeatures(absolute_y, num_tilings, y_tiles, y_tile_width) + offset;
    offset = offset + num_tilings*(y_tiles+1) - 1;
    
    % relative distance and angle from each player to the ball
    for k = 2:numel(S), 
        Pk = S{k};
        Pk_X=Pk{1}(1);
        Pk_Y=Pk{1}(2);
        Pk_theta = Pk{1}(3);
        
        angle=atan2(B_Y-Pk_Y,B_X-Pk_X)/pi*180;    %compute the angle to the ball in degrees
        
        if (angle < 0) 
            angle = angle + 360;
        end
        
        ang=angle-Pk_theta;  %from heading to ball
        
        if(ang<0)
            while(ang<0), ang=ang+360; end;    %correct the angle
        else while(ang>360), ang=ang-360;end;
        end
        
        Pk_alpha=ang;  % relative angle to the ball
        Pk_R=sqrt((B_Y-Pk_Y)^2+(B_X-Pk_X)^2);  % relative distance to the ball
        
        distances_vec{k-1} = xFeatures(Pk_R, num_tilings, distance_tiles, distance_tile_width) + offset;
        offset = offset + num_tilings*(distance_tiles+1) - 1;
        
        %angles_vec{k-1} = xFeatures(Pk_alpha, num_tilings, angle_tiles, angle_tile_width) + offset;
        angles_vec{k-1} = xFeatures(angle, num_tilings, angle_tiles, angle_tile_width) + offset;
        offset = offset + num_tilings*(angle_tiles+1) - 1;
        
    end
    
    % Get the final feature vector for each individual action
    P_num = P{3};
    
    if(action==1) % Dribble; gives coordinates of the ball, the player, and relative distances and angles to all the opponents
        F = [ball_x, ball_y, distances_vec{P_num}, angles_vec{P_num}, distances_vec{(1+num_players):(num_players + num_opponents)}, angles_vec{(1+num_players):(num_players + num_opponents)}];
    
    elseif(action>1 && action <= (1+num_players)) % PassBall(k); gives features of ball, player, features of team-mate k; features of all opponents    
        k = action -1;
        F = [ball_x, ball_y, distances_vec{P_num}, angles_vec{P_num}, distances_vec{k}, angles_vec{k}, distances_vec{(1+num_players):(num_players + num_opponents)}, angles_vec{(1+num_players):(num_players + num_opponents)}];

    elseif(action == (2+num_players)) % GetOpen; gives features for the player and all the opponents
        F = [distances_vec{P_num}, angles_vec{P_num}, distances_vec{(1+num_players):(num_players + num_opponents)}, angles_vec{(1+num_players):(num_players + num_opponents)}];
        %F = [ball_x, ball_y, distances_vec{P_num}, angles_vec{P_num}];
    elseif(action == (3+num_players)) % GoToBall; gives features of player; gives features of player and the ball
        %F = [ball_x, ball_y, distances_vec{P_num}, angles_vec{P_num}];
        F = [ball_x, ball_y, distances_vec{1:num_players},angles_vec{P_num}]; % location of the ball + locations of all temmates
        %F = [ball_x, ball_y, distances_vec{1:num_players+num_opponents},angles_vec{P_num}]; % location of the ball + locations of all temmates

    else % BlockPass(k); gives features for the player and the player k to be blocked
        k = action - (3+num_players); 
        F = [distances_vec{P_num}, angles_vec{P_num}, distances_vec{k+num_players}, angles_vec{k+num_players}];        
        %F = [distances_vec{1:num_players}, angles_vec{1:num_players}, distances_vec{k+num_players}, angles_vec{k+num_players}];        
    end

    if(record_states)
        index = numel(state_set);

        mat(:,1) = GetFullFeatures(state_set{index})';
        mat(:,2) = GetFullFeatures(S)';
        
        d = dist(mat);
        
        if(d(1,2) > 0.5),
            index = index + 1;
            state_set{index} = S;
        end
    end
end
